package com.svmc.app.task.activity;

import java.util.ArrayList;

import luyenlv.pro.screen.base.BaseActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.svmc.app.task.adapter.DbAdapter;
import com.svmc.app.task.adapter.ListHelpAdapter;
import com.svmc.app.task.model.HelpModel;
import com.svmc.app.task.tabmanager.TabGroupActivity;
import com.svmc.app.task.utils.CommonDefine;

public class HelpActivity extends BaseActivity implements OnClickListener,CommonDefine{
	private SharedPreferences mPreferences;
	private ArrayList<HelpModel> listHelp;
	ListView ls;
	TabGroupActivity parentActivity;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(getLayout(R.array.help_menu));
		findViewById(R.id.help_img_back).setOnClickListener(this);
		findViewById(R.id.help_menu_btn_feedback).setOnClickListener(this);
		ls= (ListView)findViewById(R.id.help_ls);
		mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		boolean firstTime = mPreferences.getBoolean("firstTime_help_activity", true);
		if (firstTime) { 
		    SharedPreferences.Editor editor = mPreferences.edit();
		    editor.putBoolean("firstTime_help_activity", false);
		    editor.commit();
		   DbAdapter db = new DbAdapter(this);
		   HelpModel basicFuntion = new HelpModel();
		   HelpModel applicationSetting = new HelpModel();
		   HelpModel aboutAuthor = new HelpModel();
		   basicFuntion.setNameTip(this.getString(R.string.help_menu_funtion_base));
		   basicFuntion.setConceptTip(this.getString(R.string.help_menu_basic_funtion_content));
		   applicationSetting.setNameTip(this.getString(R.string.help_menu_setting_application));
		   applicationSetting.setConceptTip(this.getString(R.string.help_menu_app_setting_content));
		   aboutAuthor.setNameTip(this.getString(R.string.help_menu_author));
		   aboutAuthor.setConceptTip(this.getString(R.string.help_menu_about_author_content));
		   db.addHelp(basicFuntion);
		   db.addHelp(applicationSetting);
		   db.addHelp(aboutAuthor);  
		}
		parentActivity = (TabGroupActivity)getParent();
		
		
	}


	@Override
	protected void onResume() {
		super.onResume();
		if (TabGroupActivity.stateActivity==DETAIL_HELP) {
			Intent restoreDetailHelp = new Intent(this, Detail_Help.class);
			restoreDetailHelp.putExtra("ID_HELP", TabGroupActivity.id_data);
			parentActivity.replaceContentView("Detail_help", restoreDetailHelp);
			TabGroupActivity.stateActivity=0;
		}else if (TabGroupActivity.stateActivity==FEEDBACK) {
			Intent restoreDetailHelp = new Intent(this, FeedBack.class);
			parentActivity.replaceContentView("feedback", restoreDetailHelp);
			TabGroupActivity.stateActivity=0;
		}else {
			updateListHelp();
		}
		
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.help_img_back:
			finish();
			break;
		case R.id.help_menu_btn_feedback:
			Intent intentFeedback= new Intent(this, FeedBack.class);
			parentActivity.startChildActivity("feedback", intentFeedback);
			break;
		default:
			break;
		}
	}
	public void updateListHelp(){
		DbAdapter db = new DbAdapter(this);
		listHelp = db.getAllHelp();
		ListHelpAdapter myadapter = new ListHelpAdapter(this, listHelp);
		ls.setAdapter(myadapter);
		listenClickListView();
	}
	public void listenClickListView() {
		final Intent intentHelp = new Intent(this, Detail_Help.class);
		ls.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				intentHelp.putExtra("ID_HELP", listHelp.get(position).getId());
				parentActivity.startChildActivity("Detail_help", intentHelp);
				
			}
		});
		
	}

	@Override
	public void clearMemory() {
		
	}

}
